package com.yanqu.road.dao.impl.activity.bowart;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.bowart.BowArtUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class BowArtUserDataDaoImpl extends TempDataObjectDao<BowArtUserData> {


    public static String getBowArtUserDataSql = "select * from t_u_activity_bow_art where activityId=? and userId=?";
    public BowArtUserData getBowArtUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getBowArtUserDataSql);
            int i = 1;
            call.setInt(i++,activityId);
            call.setLong(i++,userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertBowArtUserDataSql = "insert into t_u_activity_bow_art(activityId,userId,totalScore,curTargetIndex,contiRings,patronsId) values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(BowArtUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTotalScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCurTargetIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getContiRings()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsId()));
        if (getDb().executeNoneQuery(insertBowArtUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateBowArtUserDataSql = "update t_u_activity_bow_art set totalScore=?,curTargetIndex=?,contiRings=?,patronsId=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(BowArtUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getTotalScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCurTargetIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getContiRings()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateBowArtUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected BowArtUserData parseData(ResultSet rs) throws SQLException {
        BowArtUserData bowArtUserData = new BowArtUserData();
        bowArtUserData.setActivityId(rs.getInt("activityId"));
        bowArtUserData.setUserId(rs.getLong("userId"));
        bowArtUserData.setTotalScore(rs.getLong("totalScore"));
        bowArtUserData.setCurTargetIndex(rs.getInt("curTargetIndex"));
        bowArtUserData.setContiRings(rs.getInt("contiRings"));
        bowArtUserData.setPatronsId(rs.getInt("patronsId"));
        return bowArtUserData;
    }
}
